﻿using MiniTool.Model;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace MiniTool.Extension
{
    public static class EnumExtension
    {

        /// <summary>
        /// 返回Label值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string? ToLabel(this Enum value)
        {
            if (value == null)
            {
                return null;
            }

            FieldInfo? field = value.GetType().GetField(value.ToString());

            if (field != null)
            {
                LabelAttribute? attribute = field.GetCustomAttribute<LabelAttribute>();

                if (attribute != null)
                {
                    return attribute.Label;
                }
            }

            return null;
        }

    }
}
